<template>
  <div class="pagination">
    <button :disabled="pageNo <= 1" @click="$emit('changePageNo', pageNo - 1)">
      上一页
    </button>
    <button
      v-show="continueStartEnd.start > 1"
      @click="$emit('changePageNo', 1)"
    >
      1
    </button>

    <button v-show="continueStartEnd.start > 2">···</button>
    <!-- 
      当 v-if 与 v-for 一起使用时，v-for 具有比 v-if 更高的优先级,元素会先全部渲染,然后再条件渲染,性能不好
      最经典的解决方案就是使用计算数据 得到符合条件的值,然后再遍历
     -->
    <button
      v-for="page in continuePagesArr"
      :key="page"
      :class="{ active: page === pageNo }"
      @click="$emit('changePageNo', page)"
    >
      {{ page }}
    </button>

    <button v-show="continueStartEnd.end < totalPages - 1">···</button>
    <button
      v-show="continueStartEnd.end < totalPages"
      @click="$emit('changePageNo', totalPages)"
    >
      {{ totalPages }}
    </button>
    <button
      :disabled="pageNo === totalPages"
      @click="$emit('changePageNo', pageNo + 1)"
    >
      下一页
    </button>

    <button style="margin-left: 30px">共 60 条</button>
  </div>
</template>

<script>
export default {
  name: "Pagination",
  props: ["totalPages", "continuePages", "pageNo"],
  computed: {
    continueStartEnd() {
      const { totalPages, continuePages, pageNo } = this;
      // 先定义两个值一个是连续页的起始值，一个是连续页的结束值
      let start = 0,
        end = 0;
      if (continuePages > totalPages) {
        start = 1;
        end = totalPages;
      } else {
        // 1.首先根据当前页（pageNo）和连续页（continuePages）计算出基础的start和end
        // 连续页一定是一个奇数，连续页/2 然后向下取整，就得到了向前和向后的页码数量
        start = pageNo - Math.floor(continuePages / 2);
        end = pageNo + Math.floor(continuePages / 2);
        // 2.判断：连续页的起始如果小于1，则直接让连续页的起始变成1，然后对应的修改结束页
        if (start < 1) {
          start = 1;
          end = continuePages;
        }
        // 3.判断：连续页的结束如果大于总页数，则直接让连续页的结束变成总页数，然后对应的修改起始页
        if (end > totalPages) {
          end = totalPages;
          start = totalPages - continuePages + 1;
        }
      }

      // console.log(start, end);
      // 返回连续页的两个值组成对象
      return { start, end };
    },

    // 计算属性得到符合调件的值：连续页的页码组成的数组
    continuePagesArr() {
      //已知结束页是 continueStartEnd.end
      //已知条件是 continueStartEnd.start
      const { start, end } = this.continueStartEnd;
      const arr = [];
      for (let i = 1; i <= end; i++) {
        if (i >= start) {
          arr.push(i);
        }
      }
      return arr;
    },
  },
};
</script>

<style lang="less" scoped>
.pagination {
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
</style>